/**
 * @author mapeng
 * @version 2017-05-08
 */

(function(doc, win) {

	var pageVariables = {
		"shop" : "" , 
		"handleBack" : ""
	};

	$(doc).on("pageInit", "#storeDetailPage", function(e, pageId, $page, isBack , isForward) {
		if(isBack || isForward)	return;

		init4StoreDetailPage();
	});
	bindEvent4StoreDetailPage();

	//页面进入
	function handlePageLoad(){
		var params = CommonUtil.takeOutParamObject() || {};
		pageVariables.handleBack = params.handleBack || "back";
		params.handleBack = "";
		CommonUtil.injectParamObject(params);
	}

	//返回
	function doBack(){

		if(pageVariables.handleBack == "back"){
			$.router.back();
		}else if(pageVariables.handleBack == "load"){
			var cache = CommonUtil.takeOutParamObject() || {};
			cache.shopId = pageVariables.shopId;
			cache.handleBack = "load";
			CommonUtil.injectParamObject(cache);
			$.router.load("store_manage.html",true);
		}
	}

	function init4StoreDetailPage(){

		//页面参数
		var params = CommonUtil.takeOutParamObject() || {};
		var shopId = params.shopId;

		handlePageLoad();

		CommonUtil.sendJsonpRequest({
		// storeDetailTest.testStoreDetail({
			"url" : App.serverUrl+"/app/shop/getShopDetail.do",
			"params" : {"shopId" : shopId},
			"success" : function(result){
				if(result.success.toString() == "true"){//success
					if(result.resultObject){
						pageVariables.shop = result.resultObject;
						CommonUtil.render($("#storeDetailPage .scroll-view"), "storeDetailTPL",{"data":result.resultObject,"constants" : Constants},false);
					}
				}else{//business error
					DialogUtil.dialogWarn(result.msg);
				}
			},
			"error" : function(){//not 200
				DialogUtil.dialogWarn("请求失败，请稍候重试");
			}
		});
	}

	function bindEvent4StoreDetailPage(){

		//返回
		$(document).on("click","#storeDetailPage .header a[name='back']",function(e){
			doBack();			
		});

		//微信账户管理/开通
		$(document).on("click","#storeDetailPage .manage .right",function(e){
			manageWeixinAccount();
		});
	}

	//微信账户管理/开通
	function manageWeixinAccount(){

		//上次进行短信校验后60分钟内不需再次校验
		var smsCache = localStorageClient.load("sms",60);//data.merchantStatus == constants.store.WEIXIN_PAY_STATUS.none
		if(smsCache){
			if(pageVariables.shop.merchantStatus == Constants.store.WEIXIN_PAY_STATUS.none){//未开通
				var cache = CommonUtil.takeOutParamObject() || {};
				cache.shopId = pageVariables.shop.shopId;
				cache.type = "1"; //新增
				CommonUtil.injectParamObject(cache);
				$.router.load("weixin_open_step1.html",true);

			}else if(pageVariables.shop.merchantStatus == Constants.store.WEIXIN_PAY_STATUS.open || 
					pageVariables.shop.merchantStatus == Constants.store.WEIXIN_PAY_STATUS.frozen){//已开通/冻结
				var cache = CommonUtil.takeOutParamObject() || {};
				cache.shopId = pageVariables.shop.shopId;
				cache.merchantId = pageVariables.shop.merchantId;
				CommonUtil.injectParamObject(cache);
				$.router.load("weixin_manage.html",true);
			}

		}else{//需要短信校验

			if(pageVariables.shop.merchantStatus == Constants.store.WEIXIN_PAY_STATUS.none){//未开通
				var cache = CommonUtil.takeOutParamObject() || {};
				cache.dispatchPage = "weixin_open_step1.html";
				cache.dispatchData = {"type" : "1" , "shopId" : pageVariables.shop.shopId};
				CommonUtil.injectParamObject(cache);
				$.router.load("weixin_safe_verify.html",true);
			}else if(pageVariables.shop.merchantStatus == Constants.store.WEIXIN_PAY_STATUS.open || 
					pageVariables.shop.merchantStatus == Constants.store.WEIXIN_PAY_STATUS.frozen){//已开通/冻结
				var cache = CommonUtil.takeOutParamObject() || {};
				cache.dispatchPage = "weixin_manage.html";
				cache.dispatchData = {"shopId" : pageVariables.shop.shopId};
				CommonUtil.injectParamObject(cache);
				$.router.load("weixin_safe_verify.html",true);
			}
			
		}
	}

	var storeDetailTest = {
		"testStoreDetail" : function(options){
			options.success({
				"success" : true,
				"resultObject" : {
					"shopId" : "1",
					"mainShopName" : "麦当劳",
					"branchShopName" : "上地南路店",
					"categoryLevel1" : "美食",
					"categoryLevel2" : "西餐",
					"categoryLevel3" : "快餐",
					"contactNumber" : "11111111111",
					"address" : "北京市海淀区上地南路88号",
					"isOnline" : "T", //口碑门店状态 T ：上架 F：下架
					"serviceVersionType" : "1",	//订购版本 0：未订购 1：金元宝 2：银元宝 3：快享付
					"proUpgradeStatus" : "1", //订购状态 0：未订购 1：正常 2：已退订 3：已过期
					"expireDate" : "2017-05-08 12:12:12",	
					"alipayPayStatus" : "1", //支付宝开通状态 0：未开通 1：已开通
					"merchantStatus" : "0", //微信开通状态 0：未开通 1：已开通 2：已冻结
					"merchantId" : "001", //微信支付商户号
				}
				
			});
		}
	};

})(document, window);